package com.turman.blibrary.log;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.turman.blibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 前端日志打印器
 *
 * @author buobao
 * @since 2024/2/29 14:23
 */
public class BViewPrinter implements BLogPrinter {
    private RecyclerView recyclerView;
    private LogAdapter adapter;
    private BLogViewProvider provider;

    public BViewPrinter(Activity activity) {
        FrameLayout rootView = activity.findViewById(android.R.id.content);
        recyclerView = new RecyclerView(activity);
        adapter = new LogAdapter(LayoutInflater.from(activity));
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setAdapter(adapter);

        provider = new BLogViewProvider(rootView, recyclerView);
        provider.showFloatingView();
    }

    @Override
    public void print(@NonNull BLogConfig config, int level, String tag, @NonNull String content) {
        adapter.addItme(new BLogItemBean(System.currentTimeMillis(), level, tag, content));
        recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);
    }

    private static class LogViewHolder extends RecyclerView.ViewHolder {
        TextView tagView;
        TextView messageView;

        public LogViewHolder(@NonNull View itemView) {
            super(itemView);
            tagView = itemView.findViewById(R.id.tag);
            messageView = itemView.findViewById(R.id.message);
        }
    }

    private static class LogAdapter extends RecyclerView.Adapter<LogViewHolder> {
        private LayoutInflater inflater;
        private List<BLogItemBean> logs = new ArrayList<>();

        public LogAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        public void addItme(BLogItemBean bean) {
            logs.add(bean);
            notifyItemInserted(logs.size() - 1);
        }

        @NonNull
        @Override
        public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = inflater.inflate(R.layout.blog_item, parent, false);
            return new LogViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
            BLogItemBean bean = logs.get(position);
            int color = getHighlightColor(bean.level);

            holder.tagView.setTextColor(color);
            holder.messageView.setTextColor(color);

            holder.tagView.setText(bean.getFlattenedTag());
            holder.messageView.setText(bean.log);
        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        private int getHighlightColor(int logLevel) {
            int highlight;
            switch (logLevel) {
                case BLogType.A:
                    highlight = 0xffbbbbbb;
                    break;
                case BLogType.E:
                    highlight = 0xffff6b68;
                    break;
                case BLogType.I:
                    highlight = 0xff6a8759;
                    break;
                case BLogType.D:
                    highlight = 0xffffffff;
                    break;
                case BLogType.W:
                    highlight = 0xffbbb529;
                    break;
                default:
                    highlight = 0xffffff00;
                    break;
            }
            return highlight;
        }
    }

}
